package com.huawei.fastengine.fastview;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import com.alibaba.fastjson.JSONObject;
import com.huawei.fastengine.fastview.download.utils.log.FastViewLogUtils;
import com.huawei.fastengine.fastview.startFastappEngine.bean.Options;
import com.huawei.fastengine.fastview.startFastappEngine.bean.RpkPageInfo;
import java.util.List;
import java.util.Set;

public class RouterEventReceiver extends BroadcastReceiver {
    public static final String ACTION_ROUTER_EVENT_PUSH_FOR_WIDGET = "ACTION_ROUTER_EVENT_PUSH_FOR_WIDGET";
    public static final String KEY_PACKAGE_NAME = "keyPackageName";
    public static final String KEY_PARAMS = "keyParams";
    public static final String KEY_URI = "keyUri";
    private static final String SETTINGS_HOST = "settings";
    private static final String SETTING_LOCATION_SOURCE_MANAGER = "/location_source_manager";
    private static final String SETTING_PERMISSION_MGR = "/permission_manager";
    public static final String TAG = "RouterEventReceiver";

    private static String getPackageNameFromUri(Uri uri) {
        Object host = uri.getHost();
        if (TextUtils.isEmpty(host) || !host.equals("app")) {
            FastViewLogUtils.e(TAG, "host is wrong!");
            return "";
        }
        String path = uri.getPath();
        if (path == null || path.length() < 2) {
            return "";
        }
        path = path.substring(1);
        int indexOf = path.indexOf("/");
        return indexOf != -1 ? path.substring(0, indexOf) : path;
    }

    private static String getPageUrlFromUri(Uri uri) {
        String str = "";
        Object host = uri.getHost();
        if (TextUtils.isEmpty(host) || !host.equals("app")) {
            FastViewLogUtils.e(TAG, "host is wrong!");
            return "";
        }
        String path = uri.getPath();
        if (path == null || path.length() < 2) {
            return "";
        }
        path = path.substring(1);
        int indexOf = path.indexOf("/");
        return indexOf != -1 ? path.substring(indexOf) : str;
    }

    private static String getQueryParameter(Uri uri) {
        Set<String> queryParameterNames = uri.getQueryParameterNames();
        if (queryParameterNames == null || queryParameterNames.size() <= 0) {
            return null;
        }
        JSONObject jSONObject = new JSONObject();
        for (String str : queryParameterNames) {
            List queryParameters = uri.getQueryParameters(str);
            if (queryParameters.size() > 1) {
                jSONObject.put(str, queryParameters);
            } else if (queryParameters.size() > 0) {
                jSONObject.put(str, queryParameters.get(0));
            }
        }
        return jSONObject.toJSONString();
    }

    public void onReceive(Context context, Intent intent) {
        FastViewLogUtils.i(TAG, "RouterEventReceiver onReceive");
        if (ACTION_ROUTER_EVENT_PUSH_FOR_WIDGET.equals(intent.getAction())) {
            Options jumpOptions = FastSDKInnerInterface.getJumpOptions(context);
            String stringExtra = intent.getStringExtra(KEY_PACKAGE_NAME);
            Object stringExtra2 = intent.getStringExtra(KEY_URI);
            Object stringExtra3 = intent.getStringExtra(KEY_PARAMS);
            if (!TextUtils.isEmpty(stringExtra2)) {
                if (stringExtra2.startsWith(NavigationUtils.TEL_SCHEMA_PREF)) {
                    NavigationUtils.jumpToTel(context, stringExtra2);
                    return;
                } else if (stringExtra2.startsWith(NavigationUtils.MAIL_SCHEMA_PREF)) {
                    NavigationUtils.jumpToMail(context, stringExtra2);
                    return;
                } else if (stringExtra2.startsWith(NavigationUtils.SMS_SCHEMA_PREF)) {
                    NavigationUtils.jumpToSms(context, stringExtra2);
                    return;
                } else {
                    Uri parse = Uri.parse(stringExtra2);
                    if ("hap".equalsIgnoreCase(parse.getScheme())) {
                        String path = parse.getPath();
                        if (SETTINGS_HOST.equalsIgnoreCase(parse.getHost()) && SETTING_LOCATION_SOURCE_MANAGER.equalsIgnoreCase(path)) {
                            Intent intent2 = new Intent("android.settings.LOCATION_SOURCE_SETTINGS");
                            intent2.addFlags(268435456);
                            context.startActivity(intent2);
                            return;
                        }
                        stringExtra2 = getPageUrlFromUri(parse);
                        stringExtra = getPackageNameFromUri(parse);
                        stringExtra3 = getQueryParameter(parse);
                    } else if (NavigationUtils.jumpBySchema(context, stringExtra2, jumpOptions)) {
                        return;
                    }
                }
            }
            if (TextUtils.isEmpty(stringExtra)) {
                FastViewLogUtils.e(TAG, "RouterEventReceiver, packagename is empty!");
                return;
            }
            FastViewLogUtils.i(TAG, "packageName : " + stringExtra);
            FastViewLogUtils.i(TAG, "uri : " + stringExtra2);
            FastViewLogUtils.i(TAG, "params : " + stringExtra3);
            RpkPageInfo rpkPageInfo = new RpkPageInfo();
            rpkPageInfo.setPackageName(stringExtra);
            if (!TextUtils.isEmpty(stringExtra2)) {
                rpkPageInfo.setPageUri(stringExtra2);
            }
            if (!TextUtils.isEmpty(stringExtra3)) {
                rpkPageInfo.setPageParam(stringExtra3);
            }
            rpkPageInfo.setLatestNeed(true);
            rpkPageInfo.setCreateShortCut(FastSDKEngine.getCardShortcutPolicy(context));
            rpkPageInfo.setOptions(jumpOptions);
            OpenFastAppEngine.getInstance().openFastApp(context, rpkPageInfo, false);
        }
    }
}
